package com.example.practice.controller;

import com.example.practice.entity.Activity;
import com.example.practice.entity.JsonResponse;
import com.example.practice.entity.Rule;
import com.example.practice.entity.User;
import com.example.practice.service.ActivityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/activity")
@Api(tags = "ActivityController")
public class ActivityController {
    @Resource
    ActivityService activityService;
    @ApiOperation("查看所有活动")
    @GetMapping("/getAllActivity")
    public List<Activity> getAllActivity(){
        return this.activityService.getAllActivity();
    }
    @ApiOperation("查看是否参加")
    @GetMapping("/queryIfJoin")
    public Boolean queryIfJoin(Integer uid, Integer aid) {
        return this.activityService.queryIfJoin(uid, aid);
    }
    @ApiOperation("查看审核状态")
    @GetMapping("/queryCheckStatus")
    public Integer queryCheckStatus(Integer uid, Integer aid) {
        return this.activityService.queryCheckStatus(uid, aid);
    }
    @ApiOperation("查看属于自己的活动")
    @GetMapping("/getJoinActivity")
    public List<Activity> getJoinActivity(Integer uid) {
        List<Activity> activities = this.activityService.getJoinActivity(uid);
        return activities;
    }

    @GetMapping("/getDetail")
    public Activity getDetail(Integer aid) {
        return this.activityService.getDetail(aid);
    }

    @ApiOperation("查看没有的参加活动")
    @GetMapping("/getNotJoinActivity")
    public List<Activity> getNotJoinActivity(Integer uid) {
        List<Activity> activities = this.activityService.getNotJoinActivity(uid);
        return activities;
    }

    @ApiOperation("查看自己发布的活动")
    @GetMapping("/getReleaseActivity")
    public List<Activity> getReleaseActivity(Integer uid) {
        List<Activity> activities = this.activityService.getReleaseActivity(uid);
        return activities;
    }

    @ApiOperation("创建活动")
    @PostMapping("/createActivity")
    public JsonResponse createActivity( @RequestBody Activity activity) {
        int result = this.activityService.createActivity(activity.getCreatorId(),activity);
        if (result == 1) {
           return JsonResponse.success(result);
        } else {
            return JsonResponse.success(result);
        }
    }

    @ApiOperation("更改活动信息")
    @GetMapping("/update")
    public String updateActivity(Activity activity) {
        this.activityService.updateActivity(activity);
        return "1";
    }

    @ApiOperation("活动报名")
    @GetMapping("/Join")
    public String Join(Integer uid, Integer aid) {
        addNum(aid);
        this.activityService.studentJoin(uid, aid);
        return "成功";
    }
    @ApiOperation("增加人数")
    @GetMapping("/addNum")
    public Integer addNum(Integer aid){
        return this.activityService.addNum(aid);
    }
    @ApiOperation("减少人数")
    @GetMapping("/subNum")
    public Integer subNum(Integer aid){
        return this.activityService.subNum(aid);
    }
    @ApiOperation("活动退出")
    @GetMapping("/ExitActivity")
    public String ExitActivity(Integer uid, Integer aid) {
        subNum(aid);
        this.activityService.ExitActivity(uid, aid);
        return "成功";
    }

//    @ApiOperation("查看是否发布")
//    @GetMapping("/queryIfRelease")
//    public Boolean queryIfRelease(Integer aid){
//        return this.activityService.queryIfRelease(aid);
//    }

    @GetMapping("/updateActivityStatus")//从4到5
    public String updateActivityStatus(Integer aid) {
        return this.activityService.updateActivityStatus(aid);
    }

    @GetMapping("/updateActivityStatus1")//到1
    public String updateActivityStatus1(Integer aid) {
        return this.activityService.updateActivityStatus1(aid);
    }

    @ApiOperation("删除活动")
    @GetMapping("deleteActivity")
    public String deleteActivity(Integer aid) {
        if (this.activityService.deleteActivity(aid) == 1) {
            return "删除成功";
        } else {
            return "删除失败";
        }
    }

    @ApiOperation("活动发布")
    @GetMapping("/activityRelease")
    public String activityRelease(Rule rule, Integer sid) {
        String s = this.activityService.activityRelease(rule, sid);
        if (s.equals("both")) {
            return s;
        } else if (s.equals("teacher")) {
            return s;
        } else if (s.equals("student")) {
            return s;
        } else {
            return "fail";
        }
    }
    @ApiOperation("通过活动id搜索活动")
    @GetMapping("/queryByaid")
    public Activity queryByaid(Integer aid, Integer uid){
        Activity activities = this.activityService.queryByaid(aid, uid);
        return activities;
    }
    @ApiOperation("通过活动名称精准搜索活动")
    @GetMapping("/queryByName")
    public Activity queryByName(String name, Integer uid){
        Activity activities = this.activityService.queryByName(name, uid);
        return activities;
    }

    @ApiOperation("通过uid搜索报名成功的活动")
    @GetMapping("/getAllByUid")
    public List<Activity> getAllByUid(Integer uid) {
        List<Activity> activityList = this.activityService.getAllByUid(uid);
        return  activityList;
    }

    @ApiOperation("通过查反馈搜索活动名")
    @GetMapping("/getAct")
    public Activity getAct(Integer fid) {
        return this.activityService.getAct(fid);
    }

    @GetMapping("/getAllACT")
    public List<Activity> getAllAct() {
        List<Activity> activityList = this.activityService.getAllAct();
        return activityList;
    }
    @ApiOperation("通过活动名称精准搜索活动")
    @GetMapping("/queryIdByName")
    public Activity queryIdByName(String name){
        Activity activities = this.activityService.queryIdByName(name);
        return activities;
    }
    @GetMapping("/updateActivityStatus2")//从1到2
    public String updateActivityStatus2(Integer aid) {
        return this.activityService.updateActivityStatus2(aid);
    }

    @GetMapping("/getActivityById")
    public Activity getActivityById(Integer id){
        return this.activityService.getActivityById(id);
    }

    @GetMapping("getActivityByUid")
    public List<Activity> getActivityByUid(Integer uid){
        return this.activityService.getActivityByUid(uid);}

    @GetMapping("/getDistName")
    public List<Activity> getDistName() {
        return this.activityService.getDistName();}

    @GetMapping("/getByAid")//从1到2
    public String getByAid(Integer aid) {
        return this.activityService.getByAid(aid);
    }
    @ApiOperation("选定中学")
    @PostMapping("insertActivityAndSchool")
    public Integer insertActivityAndSchool(@RequestBody Map<String, Object> postData) {
        Integer uid = (Integer) postData.get("uid");
        List<String> sName = (List<String>) postData.get("sName");
        Integer aid = (Integer) postData.get("aid");
        // 执行相应的逻辑，使用接收到的参数进行操作
        return this.activityService.insertActivityAndSchool(uid, sName, aid);
    }

    @ApiOperation("通过aid获取Rule")
    @GetMapping("/getActRuleByAid")
    public Rule getActRuleByAid(Integer aid) {
        return this.activityService.getActRuleByAid(aid);
    }

    @ApiOperation("通过aid获取user")
    @GetMapping("/getUserNum")
    public List<User> getUserNum(Integer aid) {
        return this.activityService.getUserNum(aid);
    }

}
